package com.weixing.mall.provider.support.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.provider.model.domain.SpecValue;
import com.weixing.mall.provider.model.dto.SpecValueDto;
import com.weixing.mall.provider.service.ISpecValueService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 活动商品导入模版读取类
 *
 * @author Small
 */
public class ColorAndSizeDataListener extends AnalysisEventListener<ColorAndSizeData> {
    private static final Logger LOGGER = LoggerFactory.getLogger(ColorAndSizeData.class);
    /**
     * 每隔500条存储数据库，实际使用中可以3000条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 500;
    List<ColorAndSizeData> list = new ArrayList<ColorAndSizeData>();
    @Autowired
    private ISpecValueService specValueService;



    public ColorAndSizeDataListener() {
    }

    @Autowired
    public ColorAndSizeDataListener(ISpecValueService specValueService) {
        this.specValueService = specValueService;

    }

    @Override
    public void invoke(ColorAndSizeData data, AnalysisContext context) {
        LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
        list.add(data);
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();
        LOGGER.info("所有数据解析完成！");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {
        LOGGER.info("{}条数据，开始存储数据库！", list.size());
      for(ColorAndSizeData data:list){
            String style=data.getStyle();
          SpecValue value=specValueService.getOne(new QueryWrapper<SpecValue>().eq("spec_id",1).eq("spec_value",style).eq("spec_level",1));
           Long parentId=0L;
          if(null==value){
              SpecValue entity=new SpecValue();
              entity.setIsEnable(WhetherEnum.YES.getValue());
              entity.setOrderBy(Integer.valueOf(data.getXh()));
              entity.setSpecId(1L);
              entity.setSpecLevel(1L);
              entity.setErpId(data.getErp());
              entity.setSpecValue(data.getStyle());
              specValueService.save(entity);
              parentId=entity.getId();
          }else {
              parentId=value.getId();
          }
          SpecValueDto entity=new SpecValueDto();
          entity.setIsEnableEnum(WhetherEnum.YES);
          entity.setOrderBy(Integer.valueOf(data.getXh()));
          entity.setSpecId(1L);
          entity.setSpecLevel(2L);
          entity.setSpecValue(data.getColor());
          entity.setParentId(parentId);
          entity.setErpId(data.getErp());
          specValueService.save(entity);
          System.out.println("12321312312");
      }
        LOGGER.info("存储数据库成功！");
    }

}
